﻿using Blm.Utils.Extensions;
using Microsoft.AspNetCore.Http;
using System.IO;
using VisionCloud.Utility.Extensions;

namespace VisionCloud.Domain.Extension
{
    public static class HttpContextExtension
    {
        public static string GetBodyContent(this HttpRequest request)
        {
            string result = null;
            request.Body.Position = 0;
            using (var sr = new StreamReader(request.Body))
            {
                result = sr.ReadToEnd();
                request.Body.Position = 0;
            }
            return result;
        }

        public static string GetIp(this HttpContext httpContext)
        {
            return httpContext.GetClientUserIp(); //httpContext.Connection.RemoteIpAddress.NullToString();
        }

        public static string GetBrowser(this HttpContext httpContext)
        {
            httpContext.Request.Headers.TryGetValue("User-Agent", out var browser);
            return browser.NullToString();
        }
    }
}